<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<html xmlns:th="" lang="zh-CN">
<%@include file="/WEB-INF/pages/base/include.jsp"%>
<body>
<div id="wrapper">
    <%@include file="/WEB-INF/pages/base/menu.jsp"%>
    <div id="page-wrapper">
        <div class="row">
            <div class="col-lg-12">
                <h3 class="page-header">商品价格走势管理</h3>
            </div>
        </div>
        <div class="row">
            <div class="col-lg-12 col-md-12">
                <div class="panel panel-default">
                    <div class="panel-heading">商品价格走势管理</div>
                    <div class="panel-body inner-body">
                        <div class="main-box-search">
                            <form method="get" name="from1"  id="searchForm"  >
                                <div class="search-row">
                                    <div class="col-lg-3 col-md-6" style=" margin-bottom:15px; padding-left:0px;">
                                        <input class="form-control" type="text" id="productId" placeholder="请输入商品id">
                                    </div>
                                   <div class="search-row">
                                    <div class="col-lg-3 col-md-6" style=" margin-bottom:15px; padding-left:0px;">
                                        <input class="form-control" type="text" name="name" id="name" placeholder="请输入商品名称">
                                    </div>
                                </div>
                                </div>
                            </form>
                            <a class="btn btn-primary" onclick="toAddPrice()" style="margin-left:15px;margin-bottom:30px;">添加</a>
                        </div>
                        <div class="dataTable_wrapper">
                            <table class="table table-striped table-bordered table-hover" id="ta">
                                <thead>
                                <tr>
                                    <th class="text-center" style="width:6%;">商品编号</th>
                                    <th class="text-center" style="width:12%;">商品名称</th>
                                    <th class="text-center" style="width:6%;">商品价格</th>
                                    <th class="text-center" style="width:9%;">创建时间</th>
                                    <th class="text-center" style="width:13%;">操作</th>
                                </tr>
                                </thead>
                                <tbody  id="productLogList">
                                </tbody>
                            </table>
                        </div>
                        <div class="modal fade" id="sendModels">
                            <div class="modal-dialog">
                                <div class="modal-content message_align">
                                    <div class="modal-header">
                                        <button class="close" type="button" onclick="window.location.href='${webPath}toProductLog';" data-dismiss="modal" aria-label="Close">
                                            <span aria-hidden="true">×</span>
                                        </button>
                                        <h4 class="modal-title">价格信息</h4>
                                    </div>
                                    <div class="modal-body">
                                        <div class="form-group">
                                            <label class="col-lg-3 clo-md-3 text-right"
                                                   style="line-height:34px;">商品名称：</label>
                                            <div class="col-lg-6 col-md-6">
                                                <select class="form-control" id="productList">
                                                		<c:forEach var="item" items="${productList}" varStatus="status">
									                             <option value="${item.id}">${item.name}</option>
									                    </c:forEach>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="clearfix"></div>
                                        <div class="form-group">
                                            <label class="col-lg-3 clo-md-3 text-right"
                                                   style="line-height:34px;">商品价格：</label>
                                            <div class="col-lg-6 col-md-6">
                                                <input class="form-control" id="productPrice" type="text" placeholder="商品价格">
                                            </div>
                                        </div>                                        
                                    </div>
                                    <div class="modal-footer">
                                    	<input type="hidden" value="0" id="proIdHidden">
                                        <button class="btn btn-default" type="button" onclick="window.location.href='${webPath}toProductLog';" data-dismiss="modal">取消</button>
                                        <a class="btn btn-danger" onclick="toSave()" data-dismiss="modal">确定</a>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="text-right">
                            <ul class="pagination" id="pagination">
                                <li><a href="javascript:void(0);" onclick="" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>
				                <li><a  href="javascript:void(0);" onclick="" >1</a></li>
				                <li><a href="javascript:void(0);" onclick=""  aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>

	searchCase(0,1);
	var excelPage = 1;
	$('#searchForm').change(function(e) {
		searchCase(1,1);
	})
	
	//搜索
	function searchCase(flag,page) {
		excelPage = page;
		var name = null;
		var mallUuid = "${searchMallUuid}";
		if(flag == 1){
			name=$("#name").val();
		}
		var enabled = "${enabled}";
		if(enabled == 0){
			mallUuid = "${mallUuid}";
		}
		var productId = $("#productId").val();
		var pageSize= 10;
		var baseURL = "${webPath}/product/productLogList";
	    var json_package = {
	        "head": {},
	        "body": {
	        	"productId": productId,
	        	"name": name,
	        	"mallUuid":mallUuid,
	            "page": page,
	            "pageSize": pageSize
	        }
	    }
	    $.post(baseURL, {"json_package": JSON.stringify(json_package)}, function (result) {
	        if (typeof result == 'string') result = JSON.parse(result);
	        if (result.body.result == '1') {
	            $("#productLogList").empty();
	        } else {
	        	$("#productLogList").empty();
	        	var list = result.body.list;
	        	var count = result.body.count;
	        	var str = "";
	        	if(list.length<=0){
	        		return
	        	}
	        	
	        	for(var i=0;i<list.length;i++){
	        		str += '<tr>';
	        		str += '	<td class="text-center">'+list[i].productId+'</td>';
	        		str += '	<td class="text-center">'+list[i].name+'</td>';
	        		str += '	<td class="text-center">'+list[i].price+'</td>';
	        		str += '	<td class="text-center">'+list[i].createTime+'</td>';
	        		str += '	<td class="text-center">';
	        		str += '	<button class="btn btn-default " onclick=updates('+list[i].id+')>修改</button>&nbsp;&nbsp;<button class="btn btn-default" onclick=deletes('+list[i].id+')>删除</button>';
	        		str += '	</td>';
	        		str += '</tr>';
	        	}
	        	$("#productLogList").append(str);
	        	
	        	$("#pagination").empty();
	        	var pageString = "";
	        	var currentPage = page;
	        	var sumPage = Math.ceil(count/pageSize);
	        	pageString += '<li><a href="javascript:void(0);" onclick=searchCase(1,'+(currentPage-1)+') aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>';
	        	for(var i=1;i<=sumPage;i++){
	        		if(currentPage == i){
        				pageString += '<li><a href="javascript:void(0);" onclick=searchCase(1,'+i+') class="active">'+i+'</a></li>';
        			}else{
        				pageString += '<li><a href="javascript:void(0);" onclick=searchCase(1,'+i+')>'+i+'</a></li>';
        			}
	        	}
	        	if(sumPage==currentPage){
	        		pageString += '<li><a href="javascript:void(0);" onclick=searchCase(1,'+(currentPage)+') aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>';
	        	}else{
	        		pageString += '<li><a href="javascript:void(0);" onclick=searchCase(1,'+(currentPage+1)+') aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>';
	        	}
	        	
	        	$("#pagination").append(pageString);
	        }
	    })
	}
	
	 function toAddPrice() {
	     $('#sendModels').modal();
	 }
	
	  var toSave = function () {
	        var baseURL = "${webPath}/product/saveProductPrice";
	        var productId = $('#productList option:selected').val();
	        var productPrice = $("#productPrice").val();
	        var id=$("#proIdHidden").val();
	        if(productPrice==""){
	        	alert("请输入商品价格");
	        	return;
	        }
	        var json_package = {
	            "head": {},
	            "body": {"id": id, "productPrice": productPrice,"productId":productId}
	        };
	        $.post(baseURL, {"json_package": JSON.stringify(json_package)}, function (result) {
	            if (typeof result == 'string') result = JSON.parse(result);
	            if (result.body.result == '0') {
	            	if(id==0){//新建之后的刷新页面
	            		window.location.reload(true);
	            	}else{//更改之后重新加载页面，加载商品列表
	            		window.location.href="${webPath}toProductLog";
	            	}
	            } else {
	                alert(result.body.description)
	            }
	        })
	   };
	   
	   
	   function updates(index){
		   var baseURL = "${webPath}/product/selProPriLogById";
		    var json_package = {
		        "head": {},
		        "body": {
		        	"id": index
		        }
		    }
		    $.post(baseURL, {"json_package": JSON.stringify(json_package)}, function (result) {
		        if (typeof result == 'string') result = JSON.parse(result);
		        if (result.body.result == '1') {
		            alert(result.body.description)
		        } else {
		        	$("#productList").empty();
		        	var map=result.body.map;
		        	var str= '<option value="'+map.productId+'">'+map.name+'</option>';
		        	$("#productList").append(str);
		        	$("#productPrice").val(map.price);
		        	$("#proIdHidden").val(map.id);
		        	$('#sendModels').modal();
		        }
		    })
	   }
	   
	   
	   function deletes(index) {
	        var baseURL = "${webPath}/product/deleteProPriLog";
	        var json_package = {
	            "head": {},
	            "body": {"id": index}
	        };
	        $.post(baseURL, {"json_package": JSON.stringify(json_package)}, function (result) {
	            if (typeof result == 'string') result = JSON.parse(result);
	            if (result.body.result == '0') {
	            	searchCase(1,excelPage);
	            } else {
	                alert(result.body.description)
	            }
	        })
	   };

</script>
</body>
</html>
